package com.qth.valet.common.queue;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qth.valet.common.utils.StatusConstants;
import com.qth.valet.entity.po.DriverRecharge;
import com.qth.valet.service.DriverRechargeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 订单监听器
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DriverOrderQueueListener implements RedisDelayedQueueListener<String> {

    private final DriverRechargeService driverRechargeService;

    @Override
    public void invoke(String id) {
        // 查询订单状态，若仍是待支付，改为已超时
        DriverRecharge driverRecharge = driverRechargeService.getById(id);
        if (driverRecharge == null) {
            return;
        } else if (driverRecharge.getStatus() == StatusConstants.DRIVER_PAY_STATUS_PENDING) {
            driverRechargeService.update(new LambdaUpdateWrapper<DriverRecharge>()
                    .eq(DriverRecharge::getId, id)
                    .set(DriverRecharge::getStatus, StatusConstants.DRIVER_PAY_STATUS_TIME_OUT));
        }
    }
}
